import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webviewdemo/qgWebViewPage.dart';


typedef _JavascriptCallback = void Function(Map<String, dynamic>);
typedef JavascriptCallFunction = void Function(
    BuildContext context, Map<String, dynamic>, _JavascriptCallback? callback);

Map<String, JavascriptCallFunction> map = new Map();


void addJavascriptFunction(String funName, JavascriptCallFunction call) {
  map[funName] = call;
}

///设置交互通道
getJavascriptChannels(BuildContext context, WebViewController controller) {
  initJavascriptFunctions();
  for (var item in map.entries) {
    controller.addJavaScriptChannel("Flutter_${item.key}",
        onMessageReceived: (JavaScriptMessage message) {
          var map = jsonDecode(message.message);
          if (map is Map<String, dynamic>) {
            item.value(context, map, (backmap) async {
              String? callbackKey = map["___callback_function"];
              if (callbackKey != null) {
                String? currentUrl = await controller.currentUrl();

              }
            });
          }
        });
  }
}
void initJavascriptFunctions() {
  addJavascriptFunction("hideTitleBar", (context, vars, callback) {
    var state = context.findAncestorStateOfType<GQWebViewPageState>();
    print("object:" + state.toString());
    state?.hideTitleBar();
  });

  addJavascriptFunction("showTitleBar", (context, vars, callback) {
    var state = context.findAncestorStateOfType<GQWebViewPageState>();
    state?.showTitleBar();
  });

  addJavascriptFunction("close", (context, vars, callback) {
    var state = context.findAncestorStateOfType<GQWebViewPageState>();
    state?.close();
  });

  addJavascriptFunction("canGoBack", (context, vars, callback) {
    var state = context.findAncestorStateOfType<GQWebViewPageState>();
    callback?.call({"isBack": state?.webViewKey.currentState?.webViewController?.canGoBack()});
  });

  addJavascriptFunction("goBack", (context, vars, callback) {
    var state = context.findAncestorStateOfType<GQWebViewPageState>();
    state?.webViewKey.currentState?.webViewController?.goBack();
  });

  addJavascriptFunction("isBackNative", (context, vars, callback) {
    var state = context.findAncestorStateOfType<GQWebViewPageState>();
    state?.isBackNative = true;
  });

  addJavascriptFunction("clearCache", (context, vars, callback) {
    var state = context.findAncestorStateOfType<GQWebViewPageState>();
    state?.webViewKey.currentState?.webViewController?.clearCache();
  });


  addJavascriptFunction("padding_top", (context, vars, callback) {
    callback?.call({"bar_top": MediaQuery
        .of(context)
        .padding
        .top});
  });

  addJavascriptFunction("appLifecycleState_resumed", (context, vars, callback) async {
    var state = context.findAncestorStateOfType<GQWebViewPageState>();
    state?.callBack = callback;
    print(state?.callBack);
    // state?.callBack = callback;
  });

  /// 获取键盘高度
  addJavascriptFunction("keyboardHeight", (context, vars, callback) async {
    var state = context.findAncestorStateOfType<GQWebViewPageState>();
    state?.keyboardHeightCallBack = callback;
  });

  ///获取网络状态
  addJavascriptFunction("netWorkStatus", (context, vars, callback) async {
    MethodChannel communicateChannel = MethodChannel("flutter_callNative");
    final result = await communicateChannel.invokeMethod("getNetWorkStatus");
    callback?.call({"status": result});
  });
}
